package media

import (
	"errors"
	"server/common"

	"github.com/jiyeyuran/mediasoup-go/v2"
	"github.com/mitchellh/mapstructure"
)

// -------------------------------------------------------------------------------------------------
//
// -------------------------------------------------------------------------------------------------
func (s *MediaService) checkCreateRouterReqData(data common.CreateRouterReqData) error {
	// Check if worker ID is provided
	if data.WorkerId == 0 {
		return errors.New("worker ID is required in create router request")
	}
	// Check if service ID is provided
	if data.ServiceId == "" {
		return errors.New("service ID is required in create router request")
	}
	// Check room ID if provided
	if data.RoomId == "" {
		return errors.New("room ID is required in create router request")
	}
	// Check if worker exists
	if !s.workerMgr.WorkerExists(data.WorkerId) {
		return errors.New("worker does not exist")
	}
	// Check if service exists
	if s.serviceId != data.ServiceId {
		return errors.New("service does not exist")
	}

	return nil
}

// -------------------------------------------------------------------------------------------------
//
// -------------------------------------------------------------------------------------------------
func (s *MediaService) sendCreateRouterResponse(rid string, code int, msg string,
	data common.CreateRouterReqData, router *mediasoup.Router) {
	var caps *mediasoup.RtpCapabilities
	if router != nil {
		caps = router.RtpCapabilities()
	}

	res := common.Message{
		Cmd: common.CREATE_ROUTER_RES,
		Rid: rid,
		Data: common.ResMsgData{
			Code: code,
			Msg:  msg,
			Data: common.CreateRouterResData{
				RouterId:  router.Id(),
				ServiceId: s.serviceId,
				WorkerId:  data.WorkerId,
				RtpCaps:   caps,
			},
		},
	}
	if err := s.wsClient.Send(res); err != nil {
		LOG(rid).WithError(err).Error("failed to send create router response")
	}
}

// -------------------------------------------------------------------------------------------------
//
// -------------------------------------------------------------------------------------------------
func (s *MediaService) handleCreateRouterReq(msg common.Message) {
	LOG(msg.Rid).WithField("msg", common.ToJson(msg)).Info("handling create router request")

	var reqData common.CreateRouterReqData
	if err := mapstructure.Decode(msg.Data, &reqData); err != nil {
		LOG(msg.Rid).WithError(err).Error("failed to decode create router request data")
		s.sendCreateRouterResponse(msg.Rid, 1, err.Error(), reqData, nil)
		return
	}

	if err := s.checkCreateRouterReqData(reqData); err != nil {
		LOG(msg.Rid).WithError(err).Error("invalid create router request data")
		s.sendCreateRouterResponse(msg.Rid, 1, err.Error(), reqData, nil)
		return
	}

	router, err := s.CreateRouter(msg.Rid, reqData)
	if err != nil {
		LOG(msg.Rid).WithError(err).Error("failed to create router")
		s.sendCreateRouterResponse(msg.Rid, 1, err.Error(), reqData, nil)
		return
	}

	s.sendCreateRouterResponse(msg.Rid, 0, "success", reqData, router)
}
